<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-row :gutter="5" style="padding-top: 15px">
          <!--           左     -->
          <el-col :span="8" align="left">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                  v-hasPermi="['jbxx:unit:add']"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['jbxx:unit:edit']"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['jbxx:unit:remove']"
                >删除</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="warning"
                  plain
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport"
                  v-hasPermi="['jbxx:unit:export']"
                >导出</el-button>
              </el-col>
            </el-row>
          </el-col>
          <!--           右     -->
          <el-col :span="16" align="right" >
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item  prop="dwName">
                <el-input
                  v-model="queryParams.dwName"
                  placeholder="输入关键字筛选"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>

        <el-table border stripe v-loading="loading" :data="unitList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" type="index" :index="indexMethod" />
          <el-table-column label="单位编码" align="center" prop="dwCode" />
          <el-table-column label="单位名称" align="center" prop="dwName" />
          <el-table-column label="单位类型" align="center" prop="dwTypecode">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.jsdw_unit_type" :value="scope.row.dwTypecode"/>
            </template>
          </el-table-column>
          <el-table-column label="单位简写" align="center" prop="dwEasy" />
          <el-table-column label="换算单位" align="center" prop="dwChange" />
          <el-table-column label="换算比例" align="center" prop="changeMany" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['jbxx:unit:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['jbxx:unit:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-row :gutter="5">
          <el-col :span="24" align="right">
            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-col>
        </el-row>
      </el-col>
    </el-row>


    <!-- 添加或修改计量单位对话框 -->
    <el-dialog  :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="20">
          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="单位名称" prop="dwName">
              <el-input v-model="form.dwName" placeholder="请输入单位名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="单位类型" prop="dwTypecode">
              <el-select v-model="form.dwTypecode" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.jsdw_unit_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row :gutter="20">
          <el-col :span="12" style="padding-bottom: 10px">
            <el-form-item label="单位简写" prop="dwEasy">
              <el-input v-model="form.dwEasy" placeholder="请输入单位简写" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="换算单位" prop="dwChange" >
              <el-input @click.native="openHead" v-model="form.dwChange" placeholder="请选择" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="15" >
          <el-col :span="12" >
            <el-form-item label="换算比例" prop="changeMany" >
              <el-input style="width: 100px;" v-model="form.changeMany" placeholder="请输入" /> :
            </el-form-item>

            <el-form-item prop="changeManyTwo" style="position: absolute;left: 120px;top: 0px" >
              <el-input style="width: 100px;" v-model="form.changeManyTwo" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer"  style="padding-top: 60px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--     换算单位    -->
    <el-dialog title="选择计量单位" :visible.sync="dialogTableVisibleHead">

      <el-form :model="queryParams2" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

        <el-row>
          <el-col :span="8">
            <div>
              <el-form-item  prop="dwName">
                <el-input
                  v-model="queryParams2.dwName"
                  placeholder="请输入单位名称"
                  clearable
                  @keyup.enter.native="handleQueryListFactory"
                />
              </el-form-item>
            </div>
          </el-col>

          <el-col :span="16">
            <div align="right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryListFactory" style="background-color: rgba(0,176,192);color: #FFFFFF">查询</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>

      </el-form>

<!--  复选框方法@selection-change="handleSelectionChangeFactory"    -->
      <el-table  border stripe :data="changeManyTable" ref="multipleTableFactory" @cell-dblclick="handleDblClickRow">
<!--        <el-table-column type="selection" width="55" align="center"  />-->
        <el-table-column property="dwCode" align="center" label="单位编码" width="150"></el-table-column>
        <el-table-column property="dwName" align="center" label="单位名称" width="200"></el-table-column>
        <el-table-column label="单位类型编码" align="center" prop="dwTypecode">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.jsdw_unit_type" :value="scope.row.dwTypecode"/>
          </template>
        </el-table-column>
        <el-table-column property="dwEasy" align="center" label="单位简写"></el-table-column>
      </el-table>

      <el-row :gutter="5">
        <el-col :span="24" align="right"  style="padding-top: 30px">
          <pagination
            v-show="totalTwo>0"
            :total="totalTwo"
            :page.sync="queryParams2.pageNum"
            :limit.sync="queryParams2.pageSize"
            @pagination="getListFactory"
          />
        </el-col>
      </el-row>

<!--      <div slot="footer" class="dialog-footer" align="center" style="padding-top: 20px">-->
<!--        <el-button @click="cancelFactory" style="background-color: rgba(0,176,192);color: #FFFFFF">取 消</el-button>-->
<!--        <el-button type="primary" @click="submitFormFactory">保 存</el-button>-->
<!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import { listUnit, getUnit, delUnit, addUnit, updateUnit } from "@/api/jbxx/unit";

export default {
  name: "Unit",
  dicts: ['jsdw_unit_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalTwo: 0,
      // 计量单位表格数据
      unitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dwName: null,
        dwTypecode: null,
        dwEasy: null,
        dwChange: null,
        changeMany: null,
        changeManyTwo: null,
        dwCreate: null,
        dwCreatetime: null,
        dwUpdatetime: null
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        dwName: null,
        dwTypecode: null,
        dwEasy: null,
        dwChange: null,
        changeMany: null,
        changeManyTwo: null,
        dwCreate: null,
        dwCreatetime: null,
        dwUpdatetime: null
      },
      // 表单参数
      form: {},
      // 换算表格数据
      changeManyTable:[],
      // 表单校验
      rules: {
        dwName: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
          { max: 20, message: "单位名称长度不能超过20个字符", trigger: "blur"}
        ],
        dwTypecode: [
          { required: true, message: "单位类型不能为空", trigger: "blur" }
        ],
        dwEasy: [
          { required: true, message: "单位简写不能为空", trigger: "blur" }
        ],
        dwChange: [
          { required: true, message: "换算单位不能为空", trigger: "blur" }
        ],
        changeMany: [
          { required: true, message: "换算比率不能为空", trigger: "blur" }
        ],
        changeManyTwo: [
          { required: true, message: "换算比率不能为空", trigger: "blur" }
        ]
      },
      dialogTableVisibleHead:false,
      selectedRow: null
    };
  },
  created() {
    this.getList();
    this.getListFactory();
  },
  methods: {
    /** 查询计量单位列表 */
    getList() {
      this.loading = true;
      listUnit(this.queryParams).then(response => {

        this.unitList = response.rows;
        this.changeManyTable = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询换算单位单元列表 */
    getListFactory() {
      listUnit(this.queryParams2).then(response => {

        this.changeManyTable = response.rows;
        this.totalTwo = response.total;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.dialogTableVisibleHead = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dwCode: null,
        dwName: null,
        dwTypecode: null,
        dwEasy: null,
        dwChange: null,
        changeMany: null,
        changeManyTwo:null,
        dwCreate: null,
        dwCreatetime: null,
        dwUpdatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dwCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增计量单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dwCode = row.dwCode || this.ids
      getUnit(dwCode).then(response => {

        this.form = response.data;
        this.open = true;
        this.title = "编辑计量单位";
        if (this.form.changeMany!=null){
          var string = this.form.changeMany;
          // 使用split方法拆分字符串
          var parts = string.split(":");
          // 将拆分后的部分分别存储在变量中
          this.form.changeMany = parts[0];
          this.form.changeManyTwo = parts[1];
        }

        console.log(this.form)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {

        if (valid) {
          if (this.form.dwCode != null) {
            updateUnit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dwCodes = row.dwCode || this.ids;
      this.$modal.confirm('是否确认删除计量单位编号为"' + dwCodes + '"的数据项？').then(function() {
        return delUnit(dwCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jbxx/unit/export', {
        ...this.queryParams
      }, `unit_${new Date().getTime()}.xlsx`)
    },

    /** 序号 */
    indexMethod(index) {
      return index + 1 ;
    },

    /** 计量单位选择操作 */
    openHead(){
      this.open = false;
      this.dialogTableVisibleHead = true;

      this.$nextTick(() => {
        if (this.form.dwName!=null){
          this.selectRowById(this.form.dwName);
        }
      });

    },

    /** 计量单位修改默认选择 */
    selectRowById(dwName) {
      const row = this.changeManyTable.find(item => item.dwChange === dwName);
      if (row) {
        this.$refs.multipleTableFactory.toggleRowSelection(row);
        this.selectedRow = row;
      }

    },

    /** 换算单元搜索按钮操作 */
    handleQueryListFactory() {
      this.queryParams2.pageNum = 1;
      this.getListFactory();
    },

    /** 父级多选框选中数据 */
    handleSelectionChangeFactory(selection) {
      if (selection.length > 1) {
        // 如果选择了超过一行，则取消选择
        this.$refs.multipleTableFactory.clearSelection();
        // 保留最后一行被选中
        this.$refs.multipleTableFactory.toggleRowSelection(selection[1], true);
      } else {
        // 如果只有一行被选中，则更新selectedRow
        this.selectedRow = selection.length ? selection[0] : null;
      }

      this.ids = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length

      for (let i = 0; i < selection.length; i++) {
        this.form.dwChange = selection[i].dwName;
      }
    },

    /**换算单位取消*/
    cancelFactory() {
      this.dialogTableVisibleHead=false;
      this.open = true;
      this.$refs.multipleTableFactory.clearSelection();
      if (this.title=="新增计量单位"){
        if (this.form.dwChange != null){
          this.selectedRow = null;
          this.form.dwChange=undefined
        }
      }
    },

    /** 换算单位提交 */
    submitFormFactory(){
      this.dialogTableVisibleHead = false;
      this.open = true;
      if (this.selectedRow == null){
        this.selectedRow = null;
      }
    },

    handleDblClickRow(row) {
      this.dialogTableVisibleHead = false;
      this.open = true;
      this.form.dwChange = row.dwName;
    },
  }
};
</script>
<style>
/*弹出框圆角边框样式*/
/*.el-dialog{
  border-radius: 20px;
}*/
</style>
